package com.coke.demo.controller;

import com.coke.demo.model.Result;
import com.coke.demo.model.User;
import com.coke.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 19602
 */
@RestController
@RequestMapping("user")
public class UserController {

    private UserService userService;
    private RedisTemplate redisTemplate;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 检查用户登录信息
     *
     * @return 登录界面
     */
    @ResponseBody
    @PostMapping("/login")
    @CrossOrigin //跨域
    public Result login(User user) {
        User u = userService.checkUser(user.getUserid());
        if (u == null) {
            return new Result("404", "用户不存在");
        } else {
            if (!u.getPassword().equals(user.getPassword())) {
                return new Result("500", "密码错误");
            }
        }
        //将登录用户的ID存入Redis
        redisTemplate.opsForValue().set("userid", u.getUserid(), 1, TimeUnit.HOURS);
        return new Result("200", "登陆成功", u);
    }

    /**
     * 判断用户是否已登录，未登录需要登录账号
     *
     * @return Result
     */
    @PostMapping("/getCurrentUser")
    @CrossOrigin
    public Result getCurrentUser() {
        if (redisTemplate.opsForValue().get("userid") != null) {
            int id = (Integer) redisTemplate.opsForValue().get("userid");
            User user = userService.checkUser(id);
            if (user != null) {
                return new Result("200", "请求成功", user);
            } else {
                return new Result("500", "请求失败");
            }
        } else {
            return new Result("500", "请求失败");
        }
    }

    @PostMapping("/userExit")
    @CrossOrigin
    public Result userExit() {
        if (redisTemplate.opsForValue().get("userid") != null) {
            redisTemplate.delete("userid");
            return new Result("200", "退出成功");
        } else {
            return new Result("200", "已退出");
        }
    }

    /**
     * 用户注册
     *
     * @return 用户注册界面
     */
    @CrossOrigin
    @PostMapping("/userRegister")
    public Result teacherRegister(User user) {
        boolean flag = userService.insertUser(user);
        if (flag) {
            return new Result("200", "注册成功");
        } else {
            return new Result("500", "注册失败");
        }
    }

    /**
     * 查询所有管理员信息
     *
     * @return Result
     */
    @PostMapping("/findAllAdmin")
    @CrossOrigin
    public Result findAllAdmin() {
        List<User> admins = userService.findAllAdmin();
        return new Result("200", "查询成功", admins);
    }

    /**
     * 查询所有用户信息
     *
     * @return Result
     */
    @PostMapping("/findAllUsers")
    @CrossOrigin
    public Result findAllUsers() {
        List<User> users = userService.findAllUsers();
        return new Result("200", "查询成功", users);
    }

    /**
     * 查询所有学生信息
     *
     * @return Result
     */
    @PostMapping("/findAllStudents")
    @CrossOrigin
    public Result findAllStudents() {
        List<User> students = userService.findAllStudents();
        return new Result("200", "查询成功", students);
    }

    /**
     * 查询所有教师信息
     *
     * @return Result
     */
    @PostMapping("/findAllTeachers")
    @CrossOrigin
    public Result findAllTeachers() {
        List<User> teachers = userService.findAllTeachers();
        return new Result("200", "查询成功", teachers);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userid:用户id
     * @return Result
     */
    @PostMapping("/findById")
    @CrossOrigin
    public Result findById(Integer userid) {
        User user = userService.findById(userid);
        if (user != null) {
            return new Result("200", "请求成功", user);
        } else {
            return new Result("500", "请求失败");
        }
    }

    /**
     * 根据用户id删除用户信息
     *
     * @param userid :用户id
     * @return Result
     */
    @DeleteMapping("/deleteById")
    @CrossOrigin
    public Result deleteById(Integer userid) {
        boolean flag = userService.deleteById(userid);
        if (flag) {
            return new Result("200", "删除成功");
        } else {
            return new Result("500", "删除失败");
        }
    }

    /**
     * 一次删除多个用户
     *
     * @param ids 多个用户id
     * @return Result
     * @throws Exception:异常
     */
    @ResponseBody
    @DeleteMapping("/deleteSome")
    @CrossOrigin
    public Result deleteSome(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            userService.deleteById(id);
        }
        return new Result("200", "删除成功");
    }

    /**
     * 根据用户id修改用户信息
     *
     * @param user：用户信息
     * @return Result
     */
    @PostMapping("/updateUserInfo")
    @CrossOrigin
    public Result updateUserInfo(User user) {
        if (userService.updateUserInfo(user)) {
            return new Result("200", "修改成功");
        } else {
            return new Result("500", "修改失败");
        }
    }

    /**
     * @param priority 用户权限
     * @return Result
     */
    @ResponseBody
    @PostMapping("/countUser")
    @CrossOrigin
    public Result countUser(Integer priority) {
        int num = userService.countUser(priority);
        return new Result("200", "查询成功", num);
    }


}

